package cloud.xlh.filedynamicawareness.utils;

import lombok.extern.slf4j.Slf4j;
import org.jumpmind.symmetric.csv.CsvReader;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @ project name : example
 * @ Author: XuLeHuang
 * @ Date: 2023/1/22 22:32
 * @ Description:
 */
@Slf4j
public class FileUtil {
    /**
     * 从csv读取文件并返回字符串数组类型列表
     */
    public static List<String[]> readFromCsvFile(String filePath) {
        try {
            List<String[]> csvList = new ArrayList<String[]>();
            CsvReader reader = new CsvReader(filePath, ',', Charset.forName("GBK"));
            reader.readHeaders(); //跳过表头,不跳可以注释掉

            while (reader.readRecord()) {
                csvList.add(reader.getValues()); //按行读取，并把每一行的数据添加到list集合
            }
            reader.close();
            //log.info("读取的行数：{}", csvList.size());
            //log.info("读取的数据列表：{}", csvList);
            return csvList;
        } catch (Exception e) {
            //log.error("从csv文件读取数据失败->{}", e.getMessage());
        }
        return null;
    }
}
